package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class UserRegistration
 */
public class ShowAppointments extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowAppointments() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String inputDate = request.getParameter("date");
		String inputDoctor = request.getParameter("doctorid");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat timeformat = new SimpleDateFormat("hh:mm a");

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			String getUsedAppointmentSlotsSql = "select appointment_date from appointments "
					+ "WHERE DATE(appointment_date) = '"
					+ inputDate
					+ "' AND doctor_id=" + inputDoctor;
			rs = stmt.executeQuery(getUsedAppointmentSlotsSql);

			ArrayList<Date> usedAppointmentSlots = new ArrayList<Date>();
			// displaying records
			while (rs.next()) {
				String resultdateStr = rs.getObject(1).toString();
				Date resultdate = format.parse(resultdateStr);
				usedAppointmentSlots.add(resultdate);
			}

			ArrayList<Date> availableAppointmentSlots = new ArrayList<Date>();
			availableAppointmentSlots
					.add(format.parse(inputDate + " 09:00:00"));
			availableAppointmentSlots
					.add(format.parse(inputDate + " 10:00:00"));
			availableAppointmentSlots
					.add(format.parse(inputDate + " 11:00:00"));
			availableAppointmentSlots
					.add(format.parse(inputDate + " 12:00:00"));

			for (Date available : availableAppointmentSlots) {
				if (!usedAppointmentSlots.contains(available)) {
					out.println("<input type='radio' name='time' value='"
							+ format.format(available) + "'/>"
							+ timeformat.format(available));
					out.println("<br>");
				}
			}
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} catch (ParseException e) {
			throw new ServletException("Error parsing date.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
			out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

}
